#define SCALE			0.009
#define SAMPLES			8.0

#define SPARCLING		0.465
#define SIGMA			2.0

uniform float fSparclesPower;
uniform float fSigma;
uniform sampler2D frame;
uniform sampler2D bright;

varying vec2 TexCoord;

const float w1 = 0.524, w2 = 1.282;
const float fWeights[] = {
	// 0.524,
	// 1.048,
	// 1.282,
	// 1.572,
	// 2.096,
	// 2.564,
	// 3.846,
	// 5.128
	0.1261,
	0.2543,
	0.3864,
	0.5265,
	0.6747,
	0.8428,
	1.0390,
	1.2833
};
//const vec2 fvDirections[] = {
	//vec2(1.0, 0.0),
	//vec2(0.707, 0.707),
	//vec2(0.0, 1.0),
	//vec2(-0.707, 0.707),
	//vec2(-1.0, 0.0),
	//vec2(-0.707, -0.707),
	//vec2(0.0, -1.0),
	//vec2(0.707, -0.707)
//};

vec4 fSample(void)
{
	vec4 color = texture2D(bright, TexCoord);
	float i, value = 1.0;
	
	for (i = 0.0; i < SAMPLES; i += 1.0) {
		int index = (int)i;
		int j;
		float sample = SCALE * fWeights[index] * SIGMA;
		float weight = smoothstep(0.0, 1.0, (SAMPLES - i) / SAMPLES);
		
		//for (j = 0; j < 8; ++j) {
			//color += texture2D(bright, TexCoord + fvDirections[j] * sample) * modulate;
		//}
		
		color += texture2D(bright, TexCoord + vec2(1.0, 0.0) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(0.707, 0.707) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(0.0, 1.0) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(-0.707, 0.707) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(-1.0, 0.0) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(-0.707, -0.707) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(0.0, -1.0) * sample) * weight;
		color += texture2D(bright, TexCoord + vec2(0.707, -0.707) * sample) * weight;
		value += (8.0 * weight);
	}
	return mix((color / value), (color / (value * (1.0 - SPARCLING))), SPARCLING);
}

void main(void)
{
    vec4 color = texture2D(frame, TexCoord);
	vec4 brightColor = fSample();
   
	gl_FragColor = vec4((color + brightColor).rgb, 1.0);
}
